package com.prs.crm.dao.doc.impl;

import java.io.Serializable;
import java.sql.SQLException;
import java.util.Collection;

import org.hibernate.Criteria;
import org.hibernate.HibernateException;
import org.hibernate.Session;
import org.hibernate.criterion.Order;
import org.hibernate.criterion.Restrictions;
import org.springframework.orm.hibernate3.HibernateCallback;

import com.prs.crm.dao.doc.DocumentDao;
import com.prs.crm.dao.impl.BaseDaoImpl;
import com.prs.crm.domain.doc.Document;
import com.prs.crm.domain.pm.PmProject;
import com.prs.crm.domain.pm.PmStage;
import com.prs.crm.service.doc.DocumentService;

@SuppressWarnings("unchecked")
public class DocumentDaoImpl extends BaseDaoImpl<Document> implements DocumentDao{

	private Document document;
	private DocumentService documentService;
	
	@Override
	public Collection<Document> getDocument(final PmProject project, final PmStage stage) {
		// TODO Auto-generated method stub
		return (Collection<Document>)this.getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Document.class);
				criteria.add(Restrictions.eq("project", project));
				criteria.add(Restrictions.eq("stage", stage));
				criteria.addOrder(Order.desc("recordTime"));
				return criteria.list();
			}
			
		});
	}

	@Override
	public Collection<Document> getDocument(final PmProject project) {
		// TODO Auto-generated method stub
		return (Collection<Document>)this.getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Document.class);
				criteria.add(Restrictions.eq("project", project));
				criteria.addOrder(Order.desc("recordTime"));
				return criteria.list();
			}
			
		});
	}

	@Override
	public Collection<Document> getProjectCreateDoc(final PmProject project) {
		// TODO Auto-generated method stub
		return (Collection<Document>)this.getHibernateTemplate().executeFind(new HibernateCallback() {

			@Override
			public Object doInHibernate(Session arg0)
					throws HibernateException, SQLException {
				// TODO Auto-generated method stub
				Criteria criteria = arg0.createCriteria(Document.class);
				criteria.add(Restrictions.eq("project", project));
				criteria.add(Restrictions.isNull("stage"));
				criteria.addOrder(Order.desc("recordTime"));
				return criteria.list();
			}
			
		});
	}

	@Override
	public void saveOrUpdate(Document document) {
		this.getHibernateTemplate().update(document);
	}

	@Override
	public void del(Document ids) {
		this.getHibernateTemplate().delete(ids);
		
	}

	public Document getDocument() {
		return document;
	}

	public void setDocument(Document document) {
		this.document = document;
	}

	public DocumentService getDocumentService() {
		return documentService;
	}

	public void setDocumentService(DocumentService documentService) {
		this.documentService = documentService;
	}

	@Override
	public Document getDocument(Serializable id) {
		// TODO Auto-generated method stub
		return this.getHibernateTemplate().get(Document.class, id);
	}

}
